#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
int qpow(int x,int p,int mod){
    int res = 1;
    while(p){
        if(p & 1){
            res = 1ll*res*x%mod;            
        }
        x = 1ll*x*x%mod;
        p >>= 1;
    }
    return res;
}
int jc[N];
int jc_1[N];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T,q;
    cin>>T>>q;
    jc[0] = jc_1[0] = 1;
    int lmt = 1e6;
    for(int i = 1;i <= lmt;i++){
        jc[i] = 1ll*jc[i - 1]*i%q;
    }
    jc_1[lmt] = qpow(jc[lmt],q - 2,q);
    for(int i = lmt - 1;i >= 1;i--){
        jc_1[i] = 1ll*jc_1[i + 1]*(i + 1)%q;
    }
    while(T--){
        int n,m;
        cin>>n>>m;
        int res = 1ll*jc[n]*jc_1[n - m]%q*jc_1[m]%q;
        cout<<res<<'\n';
    }
}